package com.example.administrator.liveapp.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.bean.LiveOnlineBean;
import com.example.administrator.liveapp.bean.OfflineActivitiesBean;
import com.example.administrator.liveapp.utils.AppUrl;
import com.example.administrator.liveapp.utils.TimeChangeUtil;

import java.util.List;

import static com.example.administrator.liveapp.R.drawable.abc_edit_text_material;
import static com.example.administrator.liveapp.R.drawable.shape_reservation_corner;

/**
 * Created by Administrator on 2017/6/21 0021.
 */

public class ReservationTab_1RecyclerViewAdapter extends RecyclerView.Adapter<ReservationTab_1RecyclerViewAdapter.ViewHolder> implements View.OnClickListener{

    public List<OfflineActivitiesBean.ActivityBean> activity;
    public Context context;
    public ReservationTab_1RecyclerViewAdapter(List<OfflineActivitiesBean.ActivityBean> activity) {
        this.activity = activity;
    }


    @Override
    public ReservationTab_1RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.reservation_tab_recy_item,null);
        ViewHolder viewHolder = new ViewHolder(view);
        context = parent.getContext();
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ReservationTab_1RecyclerViewAdapter.ViewHolder holder, int position) {
        holder.activity_author.setText(activity.get(position).getActivityauthor());
        holder.activity_title.setText(activity.get(position).getActivitytitle());
        String time = TimeChangeUtil.getStrTime(activity.get(position).getActivityendtime());
        holder.activity_endtime.setText(time);
        holder.activity_place.setText(activity.get(position).getActivityplace());
        String Url = activity.get(position).getActivityimgurl();
        String ImageUrl = Url.substring(1, Url.indexOf("|"));
        Glide.with(context)
                .load(AppUrl.URL+ImageUrl)
                .into(holder.reservation_image);
        if(activity.get(position).getArrive().equals("0")){
            holder.sign_state.setText("未开始");
        }else if(activity.get(position).getArrive().equals("1")){
            holder.sign_state.setText("已开始");
        }else if(activity.get(position).getArrive().equals("2")){
            holder.sign_state.setText("缺席");
        }
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return activity.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView reservation_image;
        public TextView activity_author;
        public TextView activity_title;
        public TextView activity_endtime;
        public TextView activity_place;
        public TextView sign_state;
        public ViewHolder(View itemView) {
            super(itemView);
            reservation_image = (ImageView) itemView.findViewById(R.id.reservation_image);
            activity_author = (TextView) itemView.findViewById(R.id.activity_author);
            activity_title = (TextView) itemView.findViewById(R.id.activity_title);
            activity_endtime = (TextView) itemView.findViewById(R.id.activity_endtime);
            activity_place = (TextView) itemView.findViewById(R.id.activity_place);
            sign_state = (TextView) itemView.findViewById(R.id.sign_state);
        }
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }
    private ReservationTab_1RecyclerViewAdapter.OnItemClickListener mOnItemClickListener = null;

    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(ReservationTab_1RecyclerViewAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
